"use strict";
const Controller = require('egg').Controller;

class HomeController extends Controller {
    //前台首页文章列表
    async getArticleList() {
        let sql = 'SELECT article.id as id,' +
            'article.title as title,' +
            'article.image as image,' +
            'article.article_content as content,' +
            'article.introduce as introduce,' +
            'article.add_time as addTime,' +
            'article.view_count as view_count ,' +
            'type.type_name as typeName ' +
            'FROM article LEFT JOIN type ON article.type_id = type.id'

        const results = await this.app.mysql.query(sql)
        // this.ctx.body = await this.app.mysql.query("select * from article")
        this.ctx.body = {
            data: results
        }
    }
    //详细文章
    async getArticleInfo() {
        let id = this.ctx.params.id;
        let sql = 'select * from article where id = ' + id;
        let result = await this.app.mysql.query(sql);
        this.ctx.body = {
            data: result
        }
    }
    //算法文章
    async getArticleAlgorithm (){
        let sql = 'select * from article where type_id = 1' 
        let result = await this.app.mysql.query(sql);
        this.ctx.body = {
            data: result
        }
    }
    //知识文章
    async getKnowledge(){
        let sql = 'select * from article where type_id = 3' 
        let result = await this.app.mysql.query(sql);
        this.ctx.body = {
            data: result
        }
    }
    //生活文章
    async getLife(){
        let sql = 'select * from article where type_id = 5' 
        let result = await this.app.mysql.query(sql);
        this.ctx.body = {
            data: result
        }
    }
    //话题文章（讨论）
    async getTopic(){
        let sql = 'select * from article where type_id = 2' 
        console.log(sql);
        let result = await this.app.mysql.query(sql);
        this.ctx.body = {
            data: result
        }
    }
    //搜索文章
    async searchArticle(){
        console.log(this.ctx.params,'suos');
        let title = this.ctx.params.title;
        console.log(title,'title');
        let sql = `select * from article where title like '%${title}%'`
        console.log(sql,'sql');
        let result = await this.app.mysql.query(sql);
        console.log(result,'result');
        this.ctx.body = {
            data: result
        }
    }
}

module.exports = HomeController;